package shg.book.util
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import shg.book.view.ReadTextBook;

	public class ImageLoader extends Sprite { 
		public var url:String; 
		public var bitmap:Bitmap;
		public var byteFile:ByteArray;
		public var patternArray:Array;
		//名称key
		public var adversitName:String;
		public var index:Number=0;
		
		public function ImageLoader(loc:String = null,name:String = null,index:Number = 0) { 
			if (loc != null){ 
				url = loc; 
				adversitName=name;
				this.index=index;
				
				loadImage();
			} 
		} 
		public function selectFile(event:Event=null):void{
			//保存文件路劲
			if(event!=null)
				url=event.currentTarget.nativePath.toString();
			ReadTextBook.wallpaperPath=url;
			loadImage();
		}
		public function loadImage():void{ 
			if (url != null){ 
				var loader:Loader = new Loader(); 
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); 
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError); 
				
				var req:URLRequest = new URLRequest(url); 
				loader.load(req); 
			} 
		} 
		
		private function onComplete(event:Event):void { 
			var loader:Loader = Loader(event.target.loader); 
			var info:LoaderInfo = LoaderInfo(loader.contentLoaderInfo); 
			this.byteFile = LoaderInfo(loader.contentLoaderInfo).bytes; 
			this.bitmap = info.content as Bitmap; 
			this.dispatchEvent(new Event(Event.COMPLETE)); 
		} 
		
		private function onIoError(event:IOErrorEvent):void { 
			trace("onIoError: " + event); 
		} 
	} 

}